if(!window["vg"]) {
	vg = {};
}

if(!vg.utility) {
	vg.utility = {};
}

vg.utility.Uploader = (function() {
	var xPaths = {
		fileName : ".//*[local-name() = 'FileName']",
		guid: ".//*[local-name() = 'GUID']",
		boid: ".//*[local-name() = 'BusinessObjectID']",
		filePath : "./soap:Body/ns1:UploadFileResponse/ns1:tuple/ns1:old/ns1:upload/ns1:upload/ns1:DeleteTempFile/ns1:FilePath"
	};

    var namespaces = {
		download: "http://schemas.cordys.com/bsf/uc/webapps/1.0/ucappsutil",
		objects: "http://schemas.cordys.com/bsf/uc/webapps/1.0/objects",
		soap: "http://schemas.xmlsoap.org/soap/envelope/",
		uimodeler: "http://schemas.cordys.com/bsf/uc/webapps/1.0/uimodeler",
		ns1: "http://schemas.cordys.com/bsf/uc/webapps/1.0/ucappsutil"
    };

	var bindFunction = function (functionObj, thisObj) {
		return function () {
			functionObj.apply(thisObj, arguments);
		};
	};

	/**
	 * browseClick function is bound to the inputObj as its this object. So reference to this inside browseClick refers
	 * to the inputObj set in setUploader
	 */
	var browseClick = function () {
	    var fileExtension = "";
        this.filePath = this.uploadButton.value;
        if(this.filePath != "") {
        	this.fileName = this.filePath.substring(this.filePath.lastIndexOf("\\")+1).toLowerCase();
        	this.events["onfileselected"] && this.events["onfileselected"](this.filePath, this.fileName);
        }
	};

	var createAttachmentEntry = function (eventObject) {
		var attachRequest = cordys.loadXMLDocument('<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"><SOAP:Body><UpdateBOAttachments xmlns="http://schemas.cordys.com/bsf/uc/webapps/1.0/uimodeler"><DISPLAYNAME/><GUID/><BusinessObjectID/><DESCRIPTION/><TABLENAME/><CREATEDBY/><FILENAME/><APPLICATIONID/><ATTACHMENTGROUP/></UpdateBOAttachments></SOAP:Body></SOAP:Envelope>');
     	cordys.setNodeText(attachRequest,".//*[local-name() = 'DISPLAYNAME']", this.displayName);    
     	cordys.setNodeText(attachRequest,".//*[local-name() = 'GUID']", this.guid);    
     	cordys.setNodeText(attachRequest,".//*[local-name() = 'BusinessObjectID']", this.boid);    
     	cordys.setNodeText(attachRequest,".//*[local-name() = 'DESCRIPTION']", this.description);    
     	cordys.setNodeText(attachRequest,".//*[local-name() = 'CREATEDBY']", GlobalVariables.currentuser);    
     	cordys.setNodeText(attachRequest,".//*[local-name() = 'FILENAME']", this.fileName);    
	    cordys.setNodeText(attachRequest,".//*[local-name() = 'APPLICATIONID']", gApplicationId);
	    cordys.setNodeText(attachRequest,".//*[local-name() = 'ATTACHMENTGROUP']", this.attachmentGroup);
	    this.bdi.clear();
		this.bdi.async = "false";  
		this.bdi.request =  attachRequest;
		response = this.bdi.sendAndWait(attachRequest);
		cordys.selectXMLNode(response, ".//*[local-name() = 'UpdateBOAttachmentsResponse']");
	};

	var createBdi = function (responseHandler)
	{
	     bdiDiv = document.createElement("div");
	     document.body.appendChild(bdiDiv);
	     application.addType(bdiDiv, "wcp.library.data.BusDataIsland");
	     bdiDiv.async= true;
	     bdiDiv.onresponse= responseHandler;
	    return bdiDiv;
	};

    var uploader = function(inputObj) {		
		this.formElmtId = uploader.formElmtPrefix + (uploader.formElmtCount++);
		this.buttonId = uploader.uploadBtnPrefix + (uploader.uploadBtnCount++);
		this.uploadButtonContainer = document.getElementById(inputObj.containerId);
		this.uploadButtonContainer.innerHTML = "<form xmlns='' action='com.eibus.web.tools.upload.Upload.wcp' id='" + this.formElmtId + "' name='uploadpicture' style='float:right' enctype='multipart/form-data' method='post'><label id='loc_uploadLabel' class='cabinet'><input id='" + this.buttonId + "' type='file' size='1' width='0px' name='file1' class='file'/></label></form>";
		this.uploadButton = document.getElementById(this.buttonId);
		this.formElement = document.getElementById(this.formElmtId);
		this.uploadButton.onchange = bindFunction(browseClick, this);
	    this.uploadButton.organization = application.organization;
	    this.uploadButton.timeout = 300000000;
	    this.uploadButton.onupload = bindFunction(createAttachmentEntry, this);
	    this.bdi = createBdi();
	    this.events = [];
    };

    uploader.prototype = (function () {
    	var setBusinessObjectRecord = function (boid, guid) {
			this.guid = guid;
			this.boid = boid;
    	};
    	
    	var setFileProperties = function (fileName, displayName, description, attachmentGroup) {
    		this.fileName = fileName || "";
    		this.displayName = displayName || "";
    		this.description = description || "";
    		this.attachmentGroup = attachmentGroup || "Default Group";
    	};
    	
		var upload = function () {
		    var orgDN = application.organization;
		    var orgName = orgDN.substring(2,orgDN.indexOf(","));	
			var uploadFileNodeRequest = cordys.loadXMLDocument('<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"><SOAP:Body><UploadFile xmlns="http://schemas.cordys.com/bsf/uc/webapps/1.0/uimodeler"><FileName></FileName><FilePath>Upload:FilePath1</FilePath><BusinessObjectID></BusinessObjectID><GUID></GUID><ATTACHMENTID/><Mode>true</Mode></UploadFile></SOAP:Body></SOAP:Envelope>');
		    cordys.setTextContent(cordys.selectXMLNode(uploadFileNodeRequest, xPaths["fileName"]), this.fileName);
		    cordys.setTextContent(cordys.selectXMLNode(uploadFileNodeRequest, xPaths["guid"]), this.guid);
		    cordys.setTextContent(cordys.selectXMLNode(uploadFileNodeRequest, xPaths["boid"]), this.boid);
		    application.addType(this.uploadButton, "wcp.library.util.Upload");
		    this.uploadButton.request = uploadFileNodeRequest;
			this.uploadButton.uploadFile(this.formElement);
		};
    	
    	var setEventListener = function (eventName, func) {
    		this.events[eventName] = func;
    	};
    	
    	return {
    		setBusinessObjectRecord : setBusinessObjectRecord,
    		setFileProperties : setFileProperties,
    		upload : upload,
    		setEventListener : setEventListener
    	};
    })();

    uploader.formElmtPrefix = "browseButton";
    uploader.uploadBtnPrefix = "uploader";
    uploader.formElmtCount = 0;
    uploader.uploadBtnCount = 0;
    return uploader;
})();

scriptCallback && scriptCallback("Add Attachment");